/*
        Copyright 2018 Gaurav Kumar

        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
*/

package com.gauravk.audiovisualizersample.ui;

import com.gauravk.audiovisualizer.visualizer.BlobVisualizer;
import com.gauravk.audiovisualizersample.ResourceTable;
import com.gauravk.audiovisualizersample.util.MusicFinish;
import com.gauravk.audiovisualizersample.util.PlaySoundUtil;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.media.audio.AudioRenderer;

public class BlobAbility extends Ability {
    private BlobVisualizer mVisualizer;
    private PlaySoundUtil playSoundUtil;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_blob);

        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.rgb(66, 119, 208));

        playSoundUtil = new PlaySoundUtil();
        mVisualizer = (BlobVisualizer) findComponentById(ResourceTable.Id_blob);
        mVisualizer.setAudioSessionId(playSoundUtil.getAudioRenderer().getRendererSessionId());
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) {
                    playSoundUtil.loadSound("Ring10.wav", new MusicFinish() {
                        @Override
                        public void finish() {
                            getUITaskDispatcher().syncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    mVisualizer.setVisibility(Component.HIDE);
                                }
                            });
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        playSoundUtil.getAudioRenderer().stop();
    }
}